<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="95%">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="订单编号" prop="orderId">
          <el-input
            v-model="queryParams.orderId"
            placeholder="请输入订单编号"
            clearable
            @keyup.enter="handleQuery"
            class="!w-180px"
          />
        </el-form-item>
        <el-form-item label="内码" prop="internalCode">
          <el-input
            v-model="queryParams.internalCode"
            placeholder="请输入内码"
            clearable
            @keyup.enter="handleQuery"
            class="!w-180px"
          />
        </el-form-item>
        <el-form-item label="长度" prop="len">
          <el-input
            v-model="queryParams.len"
            placeholder="请输入长度"
            clearable
            @keyup.enter="handleQuery"
            class="!w-180px"
          />
        </el-form-item>
        <el-form-item  label="订单规格" prop="remark" v-show="Bremark" >
          <el-input
            v-model="queryParams.remark"
            placeholder="请输入订单规格"
            clearable
            @keyup.enter="handleQuery"
            class="!w-180px"
          />
        </el-form-item>
        <el-form-item label="规格型号" prop="type">
          <el-input
            v-model="queryParams.type"
            placeholder="请输入规格型号"
            clearable
            @keyup.enter="handleQuery"
            class="!w-180px"
          />
        </el-form-item>
        <el-form-item>

          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
        <!--
                <el-table-column label="序号" width="50px" type="index" />
        -->
        <el-table-column label="订单编号" align="center" prop="orderId" />
        <el-table-column label="规格型号" align="center" prop="type" />
        <el-table-column label="内码" width="250" align="center" prop="internalCode" />
        <el-table-column label="长度" align="center" prop="len" />
        <el-table-column label="订单规格"  v-show="showColumn" align="center" prop="remark" />
        <el-table-column label="最后一次收集时间"    align="center" prop="lastCollectionTimeStr" />
        <el-table-column label="操作" align="center" min-width="120px">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="deleteRow(scope.$index, scope.row.id)"
            >
              移除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>


    <template #footer>
      <form>
        <el-input v-model="formData.remark"  name="description" type="text"  placeholder="请输入备注"/>
      </form>
      <el-button @click="submitForm" type="primary">提 交</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { DeliveryReceiptApi, DeliveryReceiptVO, DeliveryReceiptConditionVO } from '@/api/hcxly/deliveryreceipt'
import { ProductProcessCompletedApi, ProductProcessCompletedVO } from '@/api/hcxly/productprocesscompleted'
import {WorkstationVO} from "@/api/hcxly/workstation";
import {useUserStore} from "@/store/modules/user";
import { ref } from 'vue';

/** 产品拨交单 表单 */
defineOptions({ name: 'DeliveryReceiptForm' })

const userStore = useUserStore()

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const loading = ref(true) // 列表的加载中
const list = ref<ProductProcessCompletedVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  orderId: undefined,
  internalCode: undefined,
  productionCode: undefined,
  remark: undefined,
  type: undefined,
  deliveryStatus: "0",
  deptId: userStore.getUser.deptId,
  excludeCompletedProcessIdList: []
})
const queryFormRef = ref() // 搜索的表单

let Bremark= true
const showColumn = ref(true); // 控制是否显示列

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formData = reactive({
  remark: ''
});

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = '新建交拨单'
  getList();
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 查询列表 */
const getList = async () => {
  loading.value = true

  if(userStore.getUser.nickname == "ww"){
    console.log( userStore.getUser.nickname)
    Bremark = true
  }

  try {
    const data = await ProductProcessCompletedApi.getProductProcessCompletedPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}


/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}


/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const deleteRow = async (index: number, id: number) => {
  queryParams.excludeCompletedProcessIdList.push(id)
  list.value.splice(index, 1)
}

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  console.log(formData.remark)

  await message.confirm('确定要提交吗？', "新建拨交单")
  const deliveryReceiptVO = {
    orderId: queryParams.orderId,
    internalCode: queryParams.internalCode,
    productionCode: queryParams.productionCode,
    type: queryParams.type,
    deptId: queryParams.deptId,
    remark: formData.remark,
    excludeCompletedProcessIdList: queryParams.excludeCompletedProcessIdList
  } as DeliveryReceiptVO
  const data = deliveryReceiptVO as unknown as DeliveryReceiptConditionVO


  const userStore = useUserStore();

  await DeliveryReceiptApi.generateDeliveryReceipt(data);
  dialogVisible.value = false
  resetQuery()
  emit('success')
}
</script>
